package com.cxrj.zhsq.userManage.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cxrj.zhsq.annotation.SystemControllerLog;
import com.cxrj.zhsq.userManage.entity.Role;
import com.cxrj.zhsq.userManage.service.RoleService;

@RestController
public class RoleController {

	@Autowired
	private RoleService roleService;

	/**
	 * 根据ID查询角色
	 * 
	 * @return
	 */
	@RequestMapping(value = "selectRoleById", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> selectRoleById(String roleId) {

		return roleService.selectRoleById(roleId);
	}

	/**
	 * 分页查询角色
	 * 
	 * @return
	 */
	@RequestMapping(value = "selectRoles", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> selectRoles(HttpServletRequest request, HttpServletResponse response) {
		return roleService.selectRolesPager(request, response);
	}

	/**
	 * 新增角色
	 * 
	 * @return
	 */
	@RequestMapping(value = "insertRole", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@SystemControllerLog(description = "新增角色")
	public Map<String, Object> insertRole(Role role, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<>();
		try {
			roleService.insertRole(role, request);
			resultMap.put("result", 1);
		} catch (Exception e) {
			resultMap.put("result", -1);
			e.printStackTrace();
		}
		return resultMap;
	}

	/**
	 * 修改角色
	 * 
	 * @return
	 */
	@RequestMapping(value = "updateRole", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@SystemControllerLog(description = "修改角色")
	public Map<String, Object> updateRole(Role role) {
		Map<String, Object> resultMap = new HashMap<>();
		try {
			roleService.updateRole(role);
			resultMap.put("result", 1);
		} catch (Exception e) {
			resultMap.put("result", -1);
			e.printStackTrace();
		}
		return resultMap;
	}

	/**
	 * 根据ID删除角色
	 * 
	 * @return
	 */
	@RequestMapping(value = "deleteRole", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@SystemControllerLog(description = "删除角色")
	public Map<String, Object> deleteRole(String roleId) {
		Map<String, Object> resultMap = new HashMap<>();
		try {
			roleService.deleteRole(roleId);
			resultMap.put("result", 1);
		} catch (Exception e) {
			resultMap.put("result", -1);
			e.printStackTrace();
		}
		return resultMap;
	}

	/**
	 * 验证账号是否使用
	 * 
	 * @return
	 */
	@RequestMapping(value = "checkRoleName", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> checkRoleName(String roleName) {

		return roleService.selectRoleName(roleName);
	}

	/**
	 * 批量删除用户
	 * 
	 * @return
	 */
	@RequestMapping(value = "deleteRolesByIds", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> deleteRolesByIds(@RequestParam(value = "roleIds[]") String[] roleIds) {
		Map<String, Object> resultMap = new HashMap<>();
		try {
			roleService.deleteRolesByIds(roleIds);
			resultMap.put("result", 1);
		} catch (Exception e) {
			resultMap.put("result", -1);
			e.printStackTrace();
		}
		return resultMap;
	}

	/**
	 * 查询所有角色
	 * 
	 * @return
	 */
	@RequestMapping(value = "selectAllRoles", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public List<Map<String, Object>> selectAllRoles(HttpServletRequest request, HttpServletResponse response) {

		return roleService.selectAllRoles(request);
	}
}
